package com.lyhengtongwl.zqsnews.ui.fragment.prentice;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.NewsDD1Adapter;
import com.lyhengtongwl.zqsnews.adapter.NewsDDAdapter;
import com.lyhengtongwl.zqsnews.base.LazyLoadBaseFragment;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.entity.NewsDD1Entity;
import com.lyhengtongwl.zqsnews.entity.NewsDDEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.ui.activity.NewsDDDetailActivity;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

@SuppressLint("ValidFragment")
public class PrenticeFragment extends LazyLoadBaseFragment {
    private String type;//全部  有效   尚未提现 0 1 2

    private String mType;//0徒弟  1徒孙
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_noRecord)
    LinearLayout ll_noRecord;
    @BindView(R.id.tv_detail)
    TextView tv_detail;
    private List<NewsDD1Entity> list = new ArrayList<>();
    private List<NewsDDEntity> list1 = new ArrayList<>();
    private NewsDD1Adapter newsDD1Adapter;
    private NewsDDAdapter newsDDAdapter;

    public PrenticeFragment(String newsType, String type) {
        this.type = newsType;
        this.mType = type;
    }

    @OnClick({R.id.tv_share
    })
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_share://隐私协议
                getActivity().finish();
                break;
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.prentice_layout;
    }

    @Override
    protected void initView(View rootView) {
        list1.clear();
        list.clear();
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(manager);
        if ("0".equals(mType)) {
            tv_detail.setText("您现在还没有徒弟，赶快去收徒吧~");
        } else {
            tv_detail.setText("您现在还没有徒孙，赶快去收徒吧~");
        }
        switch (type) {
            case "1"://有效
                newsDDAdapter = new NewsDDAdapter(getActivity(), list1, mType);
                recyclerView.setAdapter(newsDDAdapter);
                newsDDAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if ("0".equals(mType))
                            startActivity(new Intent(getActivity(), NewsDDDetailActivity.class)
                                    .putExtra("imgUrl", list1.get(position).getImgUrl())
                                    .putExtra("nikeName", list1.get(position).getNikeName())
                                    .putExtra("id", list1.get(position).getId()));

//                                    .putExtra("depositNum", list1.get(position).getDepositNum())
                    }
                });
                break;
            case "0"://全部
            case "2"://尚未提现
                newsDD1Adapter = new NewsDD1Adapter(getActivity(), list);
                recyclerView.setAdapter(newsDD1Adapter);
                break;
        }
    }

    /**
     * 只进行刷新一次
     */
    @Override
    protected void requestData() {
        super.requestData();
        //进行加载数据
        switch (type) {
            case "1"://有效
                valid();
                break;
            case "0"://全部
                all();
                break;
            case "2"://尚未提现
                notYet();
                break;
        }
    }

    //有效
    private void valid() {
        Task.getApiService().getRealPrentice().enqueue(new MyCallback<NewsBaseEntity>(getActivity()) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    String jsonStr = JSONObject.toJSONString(response.body().getData());
                    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                    JSONArray jsonArray;
                    if ("0".equals(mType)) {
                        jsonArray = jsonObject.getJSONArray("tudi");
                    } else {
                        jsonArray = jsonObject.getJSONArray("tusun");

                    }
                    if (jsonArray.size() > 0) {
                        ll_noRecord.setVisibility(View.GONE);
                        for (int i = 0; i < jsonArray.size(); i++) {
                            NewsDDEntity newsDDEntity = new NewsDDEntity();
                            newsDDEntity.setId(JSON.parseObject(jsonArray.getString(i)).getInteger("userid") + "");
                            newsDDEntity.setImgUrl(JSON.parseObject(jsonArray.getString(i)).getString("avatar"));
                            newsDDEntity.setTime(JSON.parseObject(jsonArray.getString(i)).getString("createTime"));
                            newsDDEntity.setMoney1(JSON.parseObject(jsonArray.getString(i)).getInteger("discipleShareIncome") + "");
                            newsDDEntity.setMoney2(JSON.parseObject(jsonArray.getString(i)).getInteger("todyDiscipleShareIncome") + "");
                            newsDDEntity.setNikeName(JSON.parseObject(jsonArray.getString(i)).getString("nickname"));
                            newsDDEntity.setDepositNum(JSON.parseObject(jsonArray.getString(i)).getInteger("depositNum") + "");
                            list1.add(newsDDEntity);
                        }
                        newsDDAdapter.notifyDataSetChanged();
                    } else {
                        ll_noRecord.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }

    //全部
    private void all() {
        Task.getApiService().getAllPrentice().enqueue(new MyCallback<NewsBaseEntity>(getActivity()) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    String jsonStr = JSONObject.toJSONString(response.body().getData());
                    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                    JSONArray jsonArray;
                    if ("0".equals(mType)) {
                        //获取徒弟的
                        jsonArray = jsonObject.getJSONArray("tudi");

                    } else {
                        //获取徒孙的
                        jsonArray = jsonObject.getJSONArray("tusun");

                    }
                    if (jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            NewsDD1Entity newsDD1Entity = new NewsDD1Entity();
                            newsDD1Entity.setId(JSON.parseObject(jsonArray.getString(i)).getInteger("userid") + "");
                            newsDD1Entity.setHead(JSON.parseObject(jsonArray.getString(i)).getString("avatar"));
                            newsDD1Entity.setTime(JSON.parseObject(jsonArray.getString(i)).getString("createTime"));
                            list.add(newsDD1Entity);
                        }
                        ll_noRecord.setVisibility(View.GONE);
                        newsDD1Adapter.notifyDataSetChanged();
                    } else {
                        ll_noRecord.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }

    //尚未体现
    private void notYet() {
        Task.getApiService().getNotPrentice().enqueue(new MyCallback<NewsBaseEntity>(getActivity()) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    String jsonStr = JSONObject.toJSONString(response.body().getData());
                    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                    JSONArray jsonArray;
                    if ("0".equals(mType)) {
                        //获取徒弟的
                        jsonArray = jsonObject.getJSONArray("tudi");

                    } else {
                        //获取徒孙的
                        jsonArray = jsonObject.getJSONArray("tusun");

                    }
                    if (jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            NewsDD1Entity newsDD1Entity = new NewsDD1Entity();
                            newsDD1Entity.setId(JSON.parseObject(jsonArray.getString(i)).getInteger("userid") + "");
                            newsDD1Entity.setHead(JSON.parseObject(jsonArray.getString(i)).getString("avatar"));
                            newsDD1Entity.setTime(JSON.parseObject(jsonArray.getString(i)).getString("createTime"));
                            list.add(newsDD1Entity);
                        }
                        ll_noRecord.setVisibility(View.GONE);
                        newsDD1Adapter.notifyDataSetChanged();
                    } else {
                        ll_noRecord.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }
}
